/**
 * @ClassName: SysNotifyRecord
 * @Description:淇℃伅绠＄悊
 * @author 831
 * @date 2017骞�2鏈�3鏃�
 */
package com.fzrskj.cloud.free.sys.service;

import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.tool.CStrTool;
import com.fzrskj.cloud.free.sys.bean.SysNotifyRecord;
import com.fzrskj.cloud.free.sys.utils.SysNotifyRecordUtils;
import ms.core.consts.SysErr;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysNotifyRecordService {
	
	/**
	 * 查看发送记录
	 * @param req
	 * @param resp
	 * @param p
	 * @param sid
	 * @return
	 */
	public String querySysNotifyRecordByPage(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String msgType = CJsonTool.getStrValue(p, "msgType", null);//通知方式
		String start = CJsonTool.getStrValue(p, "start", null);//开始时间
		String end = CJsonTool.getStrValue(p, "end", null);//截止时间
		String sender = CJsonTool.getStrValue(p, "sender", null);//发送方
		String receiver = CJsonTool.getStrValue(p, "receiver", null);//接收方
		int page =CJsonTool.getIntValue(p, "page", 0);
		int pageSize =CJsonTool.getIntValue(p, "pagesize", CConsts.PAGE_SIZE);

		Map<String, Object> map = new HashMap<String, Object>();
		if(!CStrTool.isBlankStr(msgType)) {
			map.put("msgType", msgType);
		}
		if(!CStrTool.isBlankStr(start)) {
			map.put("start", msgType);
		}
		if(!CStrTool.isBlankStr(end)) {
			map.put("end", msgType);
		}
		if(!CStrTool.isBlankStr(sender)) {
			map.put("sender", msgType);
		}
		if(!CStrTool.isBlankStr(receiver)) {
			map.put("receiver", msgType);
		}

		SysNotifyRecordUtils obj = new SysNotifyRecordUtils();
		int rows = obj.querySysNotifyRecordCount(map);
		String code = obj.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			return XmlCodeParser.formatResultTips(false,code);
		}

		if (rows<=0){
			return XmlCodeParser.formatResultTips(true,SysErr.SYS_DATA_NOT_FOUND);
		}

		if (rows<(page-1)*pageSize){
			return XmlCodeParser.formatResultTips(false,code);
		}
		int pages = Math.round((rows+pageSize-1)/pageSize);
		List<SysNotifyRecord> sysNotifyRecord = obj.querySysNotifyRecordList(map, page, pageSize);
		code = obj.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultPageData(code, page, pages, pageSize, rows, CJsonTool.listBeanToJson(sysNotifyRecord));
	}

	/**
	 * 查看发送记录详情
	 * @param req
	 * @param resp
	 * @param p={id:id}
	 * @param sid
	 * @return
	 */
	public String queryNotifyRecordDetails(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		int id = CJsonTool.getIntValue(p, "id", -1);
		if(id<0){
			return XmlCodeParser.formatResultTips(false,SysErr.SYS_PARAM_ERR);
		}
		SysNotifyRecordUtils obj = new SysNotifyRecordUtils();
		SysNotifyRecord sysNotifyRecord = obj.getNotifyRecord(id);
		String code = obj.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			return XmlCodeParser.formatResultTips(false, code);
		}
		return XmlCodeParser.formatResultData(true, code, CJsonTool.beanToJson(sysNotifyRecord));
	}

	/**
	 * 批量发送邮件
	 * @param req
	 * @param resp
	 * @param p={id:id}
	 * @param sid
	 * @return
	 */
	public String bathSendEmail(HttpServletRequest req, HttpServletResponse resp, String p, String sid){
		String object=CJsonTool.getStrValue(p, "object", null);			//发送对象
		String listName=CJsonTool.getStrValue(p, "listName", null);		//指定列表
		String subject = CJsonTool.getStrValue(p, "title", null);			//邮件标题
		String message = CJsonTool.getStrValue(p, "message", null);		//邮件内容
		if(CStrTool.isBlankStr(object) || CStrTool.isBlankStr(subject) || CStrTool.isBlankStr(message)){
			return XmlCodeParser.formatResultTips(false,SysErr.SYS_PARAM_ERR);
		}
		
		SysNotifyRecordUtils snrUtils=new SysNotifyRecordUtils();
		String result=snrUtils.bathSendEmail(object, listName, subject, message);	
		return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, result);
	}
}
